package com.qdsg.ysg.doctor.ui;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.rest.business.RestProxy;
import com.rest.response.TeamListResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.UtilList;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

import org.jetbrains.annotations.NotNull;

public class TeamListActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.cl_empty)
    ConstraintLayout clEmpty;
    List<TeamListResponse.Team> doctorList = new ArrayList<>();
    DoctorAdapter doctorAdapter;
    int diagType;


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_base_list;
    }

    @Override
    protected void initViews() {

        tvTitle.setText( "我的团队" );
        LinearLayoutManager manager = new LinearLayoutManager( this );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        doctorAdapter = new DoctorAdapter();
        recyclerView.setAdapter( doctorAdapter );
        recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( TeamListActivity.this, 16 );
            }
        } );

//        refreshLayout.setPrimaryColors( 0xffffffff,  0xff5481FA );
        refreshLayout.setEnableLoadMore( false );
        refreshLayout.setOnRefreshListener( refreshlayout -> {
            doctorList.clear();
            getTeamList();
        } );
    }

    @Override
    protected void initData() {
        getTeamList();
    }

    @Override
    protected void onResume() {
        getTeamList();
        super.onResume();
    }


    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder( LayoutInflater.from( TeamListActivity.this ).inflate( R.layout.item_book_doctor_team, parent, false ) );
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener( v -> {
                if (doctorList.get( position ).isLeader == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString( "id", doctorList.get( position ).id + "" );
                    startActivity( TeamManageActivity.class, bundle );
                }
            } );
            holder.tv_leader.setText( "负责人:" + doctorList.get( position ).leaderDoctorName + " " + doctorList.get( position ).leaderDoctorTitle );
            holder.tv_skill.setText( "擅长:" + doctorList.get( position ).goodAtNames );
            holder.tv_star.setText( doctorList.get( position ).score );
            holder.tv_name.setText( doctorList.get( position ).name );
            holder.ratingBar.setRating( Float.parseFloat( doctorList.get( position ).score ) );
            ImageLoaderHelper.getInstance().GlideImageLoader( TeamListActivity.this, doctorList.get( position ).thumbnailUrl, holder.img_head, R.mipmap.img_default );
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_recipel, tv_leader, tv_skill, tv_star;
            ImageView img_head, ivRecommend;
            RatingBar ratingBar;

            public Holder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
                tv_level = itemView.findViewById( R.id.tv_level );
                tv_leader = itemView.findViewById( R.id.tv_leader );
                tv_skill = itemView.findViewById( R.id.tv_skill );
                tv_star = itemView.findViewById( R.id.tv_star );
                img_head = itemView.findViewById( R.id.img_head );
                ratingBar = itemView.findViewById( R.id.ratingBar );
            }
        }
    }

    private void getTeamList() {
        RestProxy.getInstance().getTeamList( new Observer<TeamListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TeamListResponse teamListResponse) {
                if (!UtilList.isEmpty( teamListResponse.data )) {
                    refreshLayout.setVisibility( View.VISIBLE );
                    clEmpty.setVisibility( View.GONE );
                    doctorList.clear();
                    doctorList.addAll( teamListResponse.data );
                    doctorAdapter.notifyDataSetChanged();
                } else {
                    refreshLayout.setVisibility( View.GONE );
                    clEmpty.setVisibility( View.VISIBLE );
                }


            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh( false );
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh( true );
            }
        } );
    }


}
